const requestUrl = require('../../../config').requestUrl
//API 操作
let serviceData = require('../../../datas/service.js')
//import wxCharts from '../../../utils/wxcharts-min.js'
import * as echarts from '../../../ec-canvas/echarts';
let that = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ec: {},
    qyCount: 0, //企业数量
    gzhCount: 0, //公众号数量
    organName: '',
    ztArticleCount: 0, //昨日文章点击量
    szzstArr: [] //数据走势图   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this; //页外保存page指针
    this.setData({
      organName: options.organName,
    })
    wx.setStorageSync("OrganName", options.organName);
    console.log(wx.getStorageSync("OrganName"))
    this.RefreshData();
  },
  RefreshData: function () {
    this.ecComponent = this.selectComponent('#mychart-dom-line');
    this.GetQyCount();
    this.GetGzhCount();
    this.GetZtArticleCount();
    this.GetSjzstArrCount();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    console.log("刷新");
    wx.setNavigationBarTitle({
      title: '刷新中……'
    })//动态设置当前页面的标题。
    wx.showNavigationBarLoading();//在当前页面显示导航条加载动画。
    this.RefreshData();
    wx.hideNavigationBarLoading();//隐藏导航条加载动画。
    wx.stopPullDownRefresh();//停止当前页面下拉刷新。
    console.log("关闭");
    wx.setNavigationBarTitle({
      title: '统计首页'
    })//动态设置当前页面的标题。
  },
  // 点击按钮后初始化图表
  LineInit: function () {
    this.ecComponent.init((canvas, width, height) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      getLineOption(chart);
      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      this.chart = chart;
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  //获取文章总数
  GetZtArticleCount: function () {
    var self = this
    let param = {
      API_URL: requestUrl + "interZuoTinaArticleCount.action",
      data: {
        "organName": wx.getStorageSync("OrganName")
      }
    };
    console.log(param);
    serviceData.result(param).then(data => {
      console.log('接口GetZtArticleCount请求成功');
      console.log(data);
      this.setData({
        ztArticleCount: data.data.count,
      })
    }).catch(e => {
      console.log(e)
    })
  },
  //获取企业总数
  GetQyCount: function () {
    var self = this
    let param = {
      API_URL: requestUrl + "interQiyeCount.action",
      data: {
        "organName": wx.getStorageSync("OrganName")
      }
    };
    console.log(param);
    serviceData.result(param).then(data => {
      console.log('接口GetQyCount请求成功');
      console.log(data);
      this.setData({
        qyCount: data.data.count,
      })
    }).catch(e => {
      console.log(e)
    })
  },
  //获取公众号总数
  GetGzhCount: function () {
    var self = this
    let param = {
      API_URL: requestUrl + "interGzhCount.action",
      data: {
        "organName": wx.getStorageSync("OrganName")
      }
    };
    console.log(param);
    serviceData.result(param).then(data => {
      console.log('接口GetGzhCount请求成功');
      console.log(data);
      this.setData({
        gzhCount: data.data.count,
      })
    }).catch(e => {
      console.log(e)
    })
  },
  //获取数据走势图
  GetSjzstArrCount: function () {
    var self = this
    let param = {
      API_URL: requestUrl + "interDataCount.action",
      data: {
        "organName": wx.getStorageSync("OrganName")
      }
    };
    console.log(param);
    serviceData.result(param).then(data => {
      console.log('接口GetSjzstArrCount请求成功');
      console.log(data);
      this.setData({
        szzstArr: data.data.reverse(),
      })
      self.LineInit();
    }).catch(e => {
      console.log(e)
    })
  },
});
function getLineOption(chart) {
  var szzstArr = that.data.szzstArr.reverse();
  console.log(szzstArr.length);
  var rq = [];
  var wzData = [];
  var yhData = [];
  var ydData = [];
  for (var i = 0; i < szzstArr.length; i++) {
    rq.push(
      szzstArr[i].ref_date.substring(5)
    );
    wzData.push(
      szzstArr[i].articleCount
    );
    yhData.push(
      szzstArr[i].userCount
    );
    ydData.push(
      szzstArr[i].readCount
    );
  }
  console.log(wzData);
  console.log(yhData);
  console.log(ydData);
  const option = {
    // title: {
    //   text: '',
    //   left: 'center'
    // },
    color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
    legend: {
      data: ['文章数', '访问用户', '阅读数'],
      top: 20,
      left: 'center',
      // backgroundColor: 'red',
      z: 100
    },
    grid: {
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: rq,
      axisLabel: {
        interval: 0, //坐标刻度之间的显示间隔，默认就可以了（默认是不重叠）
        rotate: 38   //调整数值改变倾斜的幅度（范围-90到90）
      },
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: '文章数',
      type: 'line',
      smooth: true,
      data: wzData
    }, {
      name: '访问用户',
      type: 'line',
      smooth: true,
      data: yhData
    }, {
      name: '阅读数',
      type: 'line',
      smooth: true,
      data: ydData
    }]
  };
  chart.setOption(option);

}